
import {
  getServServiceInfoListApi,
  getChartDataApi
} from '@/services/serviceInformation.js';

export default {
  namespaced: true,
  state: {
    servServiceInfoLoading: false,
    servServiceInfoList: [],
    servServiceEchartData: {}, // 勤务信息图信息
    pagination: {
      pageNum: 1,
      pageSize: 10,
      total: 0
    }
  },
  mutations: {
    setState(state, { key, payload }) {
      state[key] = payload;
    }
  },
  actions: {
    // 获取事件监控
    async getServServiceInfoList({ commit, state, dispatch }, params) {
      commit('setState', { key: 'servServiceInfoLoading', payload: true });
      const { pageNum, pageSize } = state.pagination;
      const apiParams = {
        pageNum,
        pageSize,
        ...params
      };
      const rep = await getServServiceInfoListApi(apiParams);
      dispatch('setting/setAcarsTime', {}, { root: true });
      var dataList = [];
      if (rep.data.list.length) {
        rep.data.list.forEach(item => {
          const newObj = {};
          newObj.tailNumber = item.tailnumber;
          newObj.alert = item.isAlert ? '是' : '不是';
          newObj.location = item.pos;
          newObj.currentData = item.value;
          newObj.unit = item.unit;
          newObj.createTime = item.createTime;
          newObj.rawData = {
            alertDirection: item.alertDirection,
            maxValue: item.maxValue,
            minValue: item.minValue,
            currentValue: item.value,
            avgValue: item.avgValue,
            currentWidth: ((item.value - item.minValue) / (item.maxValue - item.minValue)) * 100,
            avgWidth: ((item.avgValue - item.minValue) / (item.maxValue - item.minValue)) * 100,
            opAlertWidth: ((item.operationalAlertLimit - item.minValue) / (item.maxValue - item.minValue)) * 100,
            serAlertWidth: ((item.serviceAlertLimit - item.minValue) / (item.maxValue - item.minValue)) * 100,
            operationalAlertLimit: item.operationalAlertLimit,
            serviceAlertLimit: item.serviceAlertLimit
          };
          newObj.serviceConfId = item.serviceConfId;
          newObj.itemName = item.item;
          dataList.push(newObj);
        });
      }
      commit('setState', { key: 'servServiceInfoList', payload: dataList });
      commit('setState', { key: 'pagination', payload: { ...state.pagination, total: rep.data.total }});
      commit('setState', { key: 'servServiceInfoLoading', payload: false });
    },
    // 获取勤务图信息
    async getServServiceEchartData({ commit, state }, params) {
      const rep = await getChartDataApi(params);
      commit('setState', { key: 'servServiceEchartData', payload: rep.data });
    }
  }
};
